package com.hisoft.controller;

import com.hisoft.client.SearchClient;
import com.hisoft.pojo.Customer;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: springcloud2020
 * @description:
 * @author: wlg
 * @create: 2021-11-14 11:52:56
 **/
@RestController
public class CustomerController {
//    @Autowired
//    private RestTemplate restTemplate;
//    @Resource
//    private EurekaClient eurekaClient;
    @Resource
    private SearchClient searchClient;

    @GetMapping("/customer")
    public String customer(){
        //获取要访问的服务信息
//        InstanceInfo search = eurekaClient.getNextServerFromEureka("SEARCH", false);
        //从服务信息中获取访问路径
//        String homePageUrl = search.getHomePageUrl();
        //使用restTemplate调用服务
//        String result = restTemplate.getForObject(homePageUrl + "/search", String.class);
//        String result = restTemplate.getForObject("http://SEARCH/search", String.class);
//        return result;
        return searchClient.search();
    }


    @GetMapping("/search/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
            @HystrixProperty(name = "execution.timeout.enabled",value = "true"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000"),
            @HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout",value = "false")
    })
    public Customer findById(@PathVariable Integer id){
        int i = 1/0;
        return searchClient.findById(id);
    }

    public Customer findByIdFallback(@PathVariable Integer id){
        return new Customer(-1,"",0);
    }

    @GetMapping("/getCustomer")
    public Customer getCustomer(@RequestParam Integer id, @RequestParam String name){
        return searchClient.getCustomer(id,name);
    }

    @GetMapping("/save")
    public Customer save(Customer customer){
        return searchClient.save(customer);
    }
}
